package com.colin.distributelock.controller;

import com.colin.distributelock.lock.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by Colin on 2020/8/31 19:55
 * email: colinzhaodong@gmail.com
 * desc:
 *
 * @author zhaod
 */
@RestController
@Slf4j
public class RedisLockController {
	@Resource
	private RedisTemplate redisTemplate;

	private final String REDIS_KEY = "distribute:lock";
	String value = UUID.randomUUID().toString().replace("-", "");

	@GetMapping("redis-lock")
	public String redisLock() {
		log.info("我进入了方法");
		log.info("我进入了锁!");
		try (RedisLock redisLock = new RedisLock(redisTemplate, REDIS_KEY, 30)) {
			Boolean lockResult = redisLock.getLock();
			if (lockResult) {
				TimeUnit.SECONDS.sleep(15);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "方法执行完成";
	}
}
